﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using NetBay.Core.Log;
#endregion

namespace Rio.Framework.Historical
{
    public static class FieldRow
    {
        #region Attributes
        /// <summary>
        /// List of values for boolean settings
        /// </summary>
        private static readonly Tuple<string, bool>[] m_tListBoolean = 
        {
            Tuple.Create("OUI", true),
            Tuple.Create("TRUE", true),
            Tuple.Create("ACTIF", true),
            Tuple.Create("ACTIVE", true),
            Tuple.Create("1", true),
            Tuple.Create("NON", false),
            Tuple.Create("FALSE", false),
            Tuple.Create("0", false)
        };

        /// <summary>
        /// Regular expression to match with French format date (ex: 15/07/2009 18:40:31)
        /// </summary>
        private static readonly Regex m_oRegexDate_JJMMYYYYspaceHHmmSS = new Regex("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)([0-9][0-9]) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])");
        #endregion

        #region Functions

        #region Publics
        /// <summary>
        /// Translate a readed values from a field row
        /// </summary>
        /// <typeparam name="T">How to interpret the value readed</typeparam>
        /// <param name="p_szReaded">the value readed</param>
        /// <returns>the value readed into a object with a good type</returns>
        public static T Translate<T>(string p_szReaded)
        {
            T l_oRet = default(T);

            try
            {
                // Check if anything to translate
                if (string.IsNullOrEmpty(p_szReaded) == false)
                {
                    // First, process for all specific conversion
                    // DateTime
                    if (typeof(T).Equals(typeof(DateTime)))
                    {
                        if( m_oRegexDate_JJMMYYYYspaceHHmmSS.IsMatch(p_szReaded) == true)
                        {
                            l_oRet = (T)Convert.ChangeType(DateTime.ParseExact(p_szReaded, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture), typeof(T));
                        }
                    }
                    // Boolean
                    else if (typeof(T).Equals(typeof(bool)))
                    {
                        // Try to detect a boolean value
                        Tuple<string, bool> l_tFind = m_tListBoolean.FirstOrDefault(item => string.Equals(p_szReaded.ToUpper(), item.Item1) == true);
                        if (l_tFind != null)
                            l_oRet = (T)Convert.ChangeType(l_tFind.Item2, typeof(T));
                    }
                    // XXX
                    //else if (typeof(T).Equals(typeof(XXX)))
                    //{
                    //}
                    // Otherwise
                    else
                    {
                        l_oRet = (T)Convert.ChangeType(p_szReaded, typeof(T));
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("p_szReaded(" + p_szReaded + ") EXCEPTION -> " + Environment.NewLine + ex);
            }
            return l_oRet;
        }

        /// <summary>
        /// Translate the field's value readed from DB to the correct type and value
        /// </summary>
        /// <param name="p_szReaded">setting's value readed from DB</param>
        /// <returns>object with the correct type and value </returns>
        public static object Translate(string p_szReaded)
        {
            object l_oRet = null;
            try
            {
                // Check empty string
                if (string.IsNullOrEmpty(p_szReaded) == true)
                    return string.Empty;

                // Try to detect a boolean value
                Tuple<string, bool> l_tFind = m_tListBoolean.FirstOrDefault(item => string.Equals(p_szReaded.ToUpper(), item.Item1) == true);
                if (l_tFind != null)
                    return l_tFind.Item2;

                // Try to detect a integer value
                int l_iParsedInteger;
                if (int.TryParse(p_szReaded, out l_iParsedInteger) == true)
                    return l_iParsedInteger;

                // Otherwise we consider it's a string value
                return p_szReaded;
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("p_szReaded(" + p_szReaded + ") EXCEPTION -> " + Environment.NewLine + ex);
            }
            return l_oRet;
        }
        #endregion

        #region Privates
        #endregion
        #endregion
    }
}
